#include<cstdio>//uncle-lu
#include<vector>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

int n, ans;
int line[16010], F[16010];
std::vector<int>son[16010];

void DFS(int x, int fa)
{
	F[x] = line[x];

	for (int i = 0; i < son[x].size(); i++) 
	{
		int v = son[x][i];
		if(v == fa)continue;
		DFS(v, x);
		F[x] = std::max(F[x], F[x] + F[v]);
	}

	ans = std::max(ans, F[x]);
	return ;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) 
		read(line[i]);

	int x, y;
	for (int i = 1; i <= n-1; i++) 
	{
		read(x);read(y);
		son[x].push_back(y);
		son[y].push_back(x);
	}

	DFS(1, 0);

	printf("%d", ans);
	return 0;
}
